package bachelorFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Environment;

public class File_Reader extends Activity {

	private String filename = "MarkerSet";
	
	
	private	boolean hasExternalStoragePrivateFile(int area_id) {
		    File file = new File(Environment.getExternalStorageDirectory().getPath(), filename+ area_id + ".csv");
		    return file.exists();		   
		}

	private boolean isExternalStorageAvailable()
	{
		boolean mExternalStorageAvailable = false;
		
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable =  true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		   
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    mExternalStorageAvailable = false;
		}
		return mExternalStorageAvailable;
	}
	
	public List<Marker_Information> getInfos(int area_id){
			
		if(isExternalStorageAvailable() && hasExternalStoragePrivateFile(area_id)){
		
		List<Marker_Information> m_infos = new ArrayList<Marker_Information>();
		String path = Environment.getExternalStorageDirectory().getPath();
		
	
			try {
				BufferedReader in = new BufferedReader(new FileReader(path + "/" + filename + area_id + ".csv"));
				String line = null;
				String trennzeichen = "[;]";
				String[] line_content = null;		
			
					while ((line = in.readLine()) != null) {
						line_content = line.split(trennzeichen);
					
						Marker_Information m_info = new Marker_Information(
								line_content[0], //Marker
								line_content[1], //Pfad
								line_content[2], //Textur
								Float.parseFloat(line_content[3]) , 
								Float.parseFloat(line_content[4]), 
								Float.parseFloat(line_content[5]), 
								Float.parseFloat(line_content[6]), 
								Float.parseFloat(line_content[7]),
								Float.parseFloat(line_content[8]));
			
						m_infos.add(m_info);
					}
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			return m_infos;
		}
		else 
			return null;
		}
}
